package com.example.rxjavademo;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.jakewharton.rxbinding3.view.RxView;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.functions.Function9;
import io.reactivex.internal.functions.Functions;
import io.reactivex.schedulers.Schedulers;

import static java.lang.Thread.sleep;


public class MainActivity extends AppCompatActivity {

    private Button bt01;
    private Button bt02;
    private Button bt03;
    private Button bt04;
    private Button bt05;
    private Button bt06;
    private Button bt07;
    private Button bt08;
    private Button bt09;


    @SuppressLint("CheckResult")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bt01 = findViewById(R.id.bt_01);
        bt02 = findViewById(R.id.bt_02);
        bt03 = findViewById(R.id.bt_03);
        bt04 = findViewById(R.id.bt_04);
        bt05 = findViewById(R.id.bt_05);
        bt06 = findViewById(R.id.bt_06);
        bt07 = findViewById(R.id.bt_07);
        bt08 = findViewById(R.id.bt_08);
        bt09 = findViewById(R.id.bt_09);

//
//        RxView.clickEvents(button)
//                .throttleFirst(500, TimeUnit.MILLISECONDS)
//                .subscribe(clickAction);
        RxView.clicks(bt09)
                .throttleFirst(2, TimeUnit.SECONDS)
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        Log.d("HHH", "@@@@@@@@@@");
                        Toast.makeText(MainActivity.this, "11111111111", Toast.LENGTH_LONG).show();
                    }
                });

        RxView.longClicks(bt09)
                .subscribe((Consumer<Object>) o -> Toast.makeText(MainActivity.this, "2222222222", Toast.LENGTH_LONG).show());
        bt01.setOnClickListener(this::demo1);
        bt02.setOnClickListener(this::demo2);
        bt03.setOnClickListener(this::demo3);
        bt04.setOnClickListener(this::demo4);
        bt05.setOnClickListener(this::demo5);
        bt06.setOnClickListener(this::demo6);
        bt07.setOnClickListener(this::demo7);
        bt08.setOnClickListener(this::demo8);
    }

    private void demo8(View view) {
        List<Student> student = new ArrayList<>();
        List<Student.KeCheng> keChengs = new ArrayList<>();
        keChengs.add(new Student.KeCheng("AAA"));
        keChengs.add(new Student.KeCheng("BBB"));
        keChengs.add(new Student.KeCheng("CCC"));
        student.add(new Student("黎明", keChengs));
        student.add(new Student("xiaohong", keChengs));
//        Observable.fromIterable(student).map(new Function<Student, String>() {
//            @Override
//            public String apply(Student student) throws Exception {
//                return student.getName();
//            }
//        }).subscribe(new Consumer<String>() {
//            @Override
//            public void accept(String s) throws Exception {
//                Log.d("HHH", s);
//            }
//        });
//        Observable.fromIterable(student)
//                .subscribe(new Consumer<Student>() {
//                    @Override
//                    public void accept(Student s) throws Exception {
//                        for (int i = 0; i < s.getKeChengs().size(); i++) {
//                            Log.d("HHH", s.getKeChengs().get(i).getKcname());
//
//                        }
//                    }
//                });
        Observable.fromIterable(student)
                .flatMap(new Function<Student, Observable<Student.KeCheng>>() {
                    @Override
                    public Observable<Student.KeCheng> apply(Student student) throws Exception {
                        return Observable.fromIterable(student.getKeChengs());
                    }
                })
                .subscribe(new Consumer<Student.KeCheng>() {
                    @Override
                    public void accept(Student.KeCheng s) throws Exception {
                        Log.d("HHH", s.getKcname());
                    }
                });


    }


    static class Student {
        String name;
        List<KeCheng> keChengs;

        public Student(String name, List<KeCheng> keChengs) {
            this.name = name;
            this.keChengs = keChengs;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public List<KeCheng> getKeChengs() {
            return keChengs;
        }

        public void setKeChengs(List<KeCheng> keChengs) {
            this.keChengs = keChengs;
        }

        public Student() {

        }

        static class KeCheng {
            String kcname;

            public KeCheng(String kcname) {
                this.kcname = kcname;
            }

            public String getKcname() {
                return kcname;
            }

            public void setKcname(String kcname) {
                this.kcname = kcname;
            }
        }
    }

    private void demo7(View view) {
    }

    private void demo6(View view) {
        Observable.create((ObservableOnSubscribe<Drawable>) emitter -> {

            for (int i = 0; i < 10; i++) {
                Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher_background);

                if (i == 5) {
                    sleep(3000);
                }
                if (i == 7) {
                    Log.d("HHH", "复制到SD卡上");
                }
                if (i == 8) {
                    Log.d("HHH", "上传第八张图片到服务器");
                    sleep(1000);

                }
                emitter.onNext(drawable);
            }

        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Drawable>() {
                    @Override
                    public void accept(Drawable drawable) throws Exception {
                        Log.d("HHH", "显示图片");
                        Toast.makeText(MainActivity.this, "显示图片", Toast.LENGTH_SHORT).show();
                    }
                });


    }

    private void demo5(View view) {
        Observable.create(
                (ObservableOnSubscribe<String>) emitter -> {
                    emitter.onNext("连载1");
                    emitter.onNext("连载2");
                    sleep(3000);
                    emitter.onNext("连载3");
                    emitter.onComplete();
                    emitter.onNext("连载4");

                })
                .subscribeOn(Schedulers.io())//执行在io线程
                .observeOn(AndroidSchedulers.mainThread())//回调在主线程
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(String value) {
//                        System.out.println("HHH" + value);
                        Log.d("HHH", value);
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onComplete() {
                    }
                });

    }


    private void demo4(View view) {
        Observable.just(111) // 输入类型 String
                .map(integer -> String.valueOf(integer))
                .subscribeOn(Schedulers.io()) // 指定 subscribe() 发生在 IO 线程
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(s -> Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show());
    }


    private void demo3(View view) {
        List<String> list = new ArrayList<>();
        list.add("AA");
        list.add("BB");
        list.add("DD");
        String[] value = {"A", "B", "C"};

        Observable.just("A", "NB").subscribe(new Consumer<String>() {
            @Override
            public void accept(String s) throws Exception {
                Log.d("HH", s + "just");
            }
        });
        Observable.fromArray(value).subscribe(s -> Log.d("HH", s + "fromArray"), throwable -> Log.d("HH", "抛出异常"));
    }

    private void demo2(View view) {
        Observable<String> observable = Observable.create(
                (ObservableEmitter<String> emitter) -> {
                    emitter.onNext("A");
                    emitter.onNext("B");
                    emitter.onNext("C");
                    emitter.onComplete();
                }
        );
        observable.subscribe(
                (String s) -> Log.d("HHH", s),
                Functions.<Throwable>emptyConsumer(),
                () -> Log.d("HHH", "onComplete")
        );
    }

    private void demo1(View view) {
        Observable<String> observable = Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> emitter) throws Exception {
                emitter.onNext("A");
                emitter.onNext("B");
                emitter.onNext("C");
                emitter.onComplete();
                //onComplete执行以后所有的方法都不会再执行了
                emitter.onNext("D");

            }
        });
        Observer<String> observer = new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                Log.d("HHH", "onSubscribe");
            }

            @Override
            public void onNext(String s) {

                Log.d("HHH", "onNext" + s);

            }

            @Override
            public void onError(Throwable e) {
                Log.d("HHH", "onError");

            }

            @Override
            public void onComplete() {
                Log.d("HHH", "onComplete");

            }
        };
        observable.subscribe(observer);
    }
}
